﻿/*
LocalSharedObject 作為 Flash 專用的 server side cookie 而存在.
於 SharedObject Name 相同的情況下能取得同一值.

Usage:
var _CL:cacheLocal = new cacheLocal();
_CL.load('NAME'); // if null it will be override ('local');
trace( _CL.data ); // if server have some value, it will show.
_CL.data = ["hello","world",true,1234];
_CL.save();	// you must save to force it replace the value on server.

_CL.RELEASE(); // Only release local's var.


*/
package com.cf.core.Utils{
	import flash.net.SharedObject;
	public class cacheLocal{
		private var G_inited:Boolean;
		private var G_SO:SharedObject;
		private var G_cacheName:String="local";
		public function cacheLocal():void{
			G_inited=false;
		}//fn
		public function load(_cacheName:String=""):void{
			// load the data, or create it.
			if( _cacheName=="" && !G_inited)
				_cacheName=G_cacheName;
			else
				G_cacheName = _cacheName;
			// force Update value on next "load"
			// FIX : Ram bug, control overflow.
			G_SO=null;
			G_SO = SharedObject.getLocal( G_cacheName );
			trace("cacheLocal : cache -> "+ G_cacheName);
			G_inited=true;
		}//fn
		public function save():void{
			// save to local
			G_SO.flush();
			G_inited=false;
			trace("cacheLocal : "+ G_cacheName+" saved");
		}//fn
		public function RELEASE():void{
			// clear all cookie.
			G_SO.clear();
			G_SO=null;
			G_inited=false;
		}//fn
		// 注意:資料都是儲存在.data底下, 要增加Object物件的子變數不用另外宣告, 直接給值就會建立
		public function get data():Object{
			if(G_inited){
				return G_SO.data;
			}else{
				this.load();
				return G_SO.data;
			}//fn
		}//fn
		// size為所佔空間, 0代表Shared object不存在
		public function get size():uint{return G_SO.size}
	}//class
}//package